import { ipcMain, screen, BrowserWindow } from 'electron/main'
import { webPath, webUrl, isDev, adminPath } from '../../config/index.js'

import { createWindow } from '../index.js'

ipcMain.on('openWeb', (event, path) => {
  console.log('[ipcMain] openWeb', path)

  const lastWin = BrowserWindow.getAllWindows().pop()
  const [x, y] = lastWin.getPosition()

  const win = createWindow({
    x: x + 88,
    y,
  })

  if (isDev) {
    const url = `${webUrl}/${path}`
    win.loadURL(url)
    // win.webContents.openDevTools()
  } else {
    // win.loadFile(webPath + '/#/' + path)
    win.loadURL(`file://${webPath}` + '#/' + path)
  }
})

ipcMain.on('openAdmin', (event, path) => {
  console.log('adminPath', adminPath)

  console.log('[ipcMain] openAdmin', path)

  const file = `file://${adminPath}` + '#/' + path

  console.log('file', file)

  const lastWin = BrowserWindow.getAllWindows().pop()
  const [x, y] = lastWin.getPosition()

  const win = createWindow({
    x: x + 88,
    y,
  })

  win.loadURL(file)
})
